# 你想构建一个能支持迭代操作的自定义对象，并希望找到一个能实现迭代协议的简单方法。
# 下面的示例展示了一个树的深度优先遍历实现

class Node:
    def __init__(self, value):
        self._value = value
        self._children = []

    def __repr__(self):
        return 'Node({!r})'.format(self._value)

    def add_child(self, node):
        self._children.append(node)

    def __iter__(self):
        return iter(self._children)

    def depth_first(self):
        yield self
        for ch in self:
            yield from ch.depth_first()

if __name__ == '__main__':
        root = Node(0)
        ch1 = Node(1)
        ch2 = Node(2)
        root.add_child(ch1)
        root.add_child(ch2)
        ch1.add_child(Node(3))
        ch1.add_child(Node(4))
        ch2.add_child(Node(5))
        for ch in root.depth_first():
            print(ch)
